<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/BaseApiController.php';
class classroom extends  BaseApiController
{
   public function index(){

   }
   
   public function list_by_page(){
	   $token=$this->verifyToken();
	   if($token){
			 $keys=[
				 ["name","option","姓名",""]
			 ];
			 
			 $data=$this->getFields($keys);

			 foreach($data as $key=>$value){
				 $this->db->like($key,$value); 
			 }
			 $this->db->where("schcode",$token["schcode"]);
			 $res=$this->db->select("count(*) as count")->get("classroom")->row_array();
			 $numrows=(int)$res["count"];
			 
			 foreach($data as $key=>$value){
				 $this->db->like($key,$value); 
			 }
			 $this->db->where("schcode",$token["schcode"]);
			 $rows=$this->db->get("classroom")->result_array();
			 
			 $this->WRITE("code",1);
			 $this->WRITE("numrows",$numrows);
			 $this->WRITE("list",$rows);
			 $this->RESPONSE();	 
		}
   }
   
   public function list_option(){
	   $token=$this->verifyToken();
	   if($token){
		   $this->db->where("schcode",$token["schcode"]);
		   $rows=$this->db->get("classroom")->result_array();
		   $this->WRITE("code",1);
		   $this->WRITE("list",$rows);
		   $this->RESPONSE();	 
	   }
   }
   
   public function create(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
				 ["name","required","教室名称",""],
				 ["maxnum","required","可容纳人数",""],
				 ["memo","option","备注",""],
		   ];
		   $data=$this->getFields($keys);
		   $data["schcode"]=$token["schcode"];
		   $res=$this->db->insert("classroom",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","新增成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","新增失败!");
			   $this->RESPONSE();	
		   }
	   }
   }
   
   public function update(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
				["roomid","required","教室ID",""],
				["name","required","教室名称",""],
				["maxnum","required","可容纳人数",""],
				["memo","option","备注",""],
		   ];
		   
		   $data=$this->getFields($keys);
		   
		   $res=$this->db->where("roomid",$data["roomid"])->update("classroom",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","删除成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","删除失败!");
			   $this->RESPONSE();	
		   }
	   }
   
   }
   
   
   public function delete(){ 
	   $token=$this->verifyToken();
	   if($token){
		   $data=$this->getFields(array(["roomid","required","教室索引",""]));
		   $res=$this->db->where("roomid",$data["roomid"])->delete("classroom",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","删除成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","删除失败!");
			   $this->RESPONSE();	
		   }
		}
   }
}